//
//  XS_AdmissionVC.swift
//  XueDao
//
//  Created by Mac on 2021/1/22.
//

import UIKit

class XS_AdmissionVC: XS_BaseVC {

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var contentView: UIView!
    
    @IBOutlet weak var iconImageView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var codeImageView: UIImageView!
    @IBOutlet weak var learnBtn: UIButton!
    
    @IBOutlet weak var leftNameView: UIView!
    @IBOutlet weak var rightNameView: UIView!
    
    var responseModel : XS_BuyResponseModel?
    
    var courseClassification = ""
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        title = "入学通知书"
        navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: #imageLiteral(resourceName: "icon_分享"), style: .plain, target: self, action: #selector(shareBtnAction))
        navigationItem.leftBarButtonItem = UIBarButtonItem.init(image: #imageLiteral(resourceName: "icon_返回"), style: .plain, target: self, action: #selector(backBtnAction))
        scrollView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: .bottomHeight, right: 0)
        
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
        
        iconImageView.sd_setImage(with: URL.init(string: XS_Singleton.shared.infoModel?.userBO.userImgSrc ?? ""), completed: nil)
        nameLabel.text = XS_Singleton.shared.infoModel?.userBO.username
        let date = Date.init()
        let formatter = DateFormatter.init()
        formatter.dateFormat = "yyyy-MM-dd"
        
        let titleAttDict: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : UIFont.xsFont(name: .regular, 16),
                                                            NSAttributedString.Key.foregroundColor : UIColor.gray]
        
        let timeAtt = NSMutableAttributedString.init(string: "在 ", attributes: titleAttDict)
        timeAtt.append(NSMutableAttributedString.init(string: formatter.string(from: date)))
        timeLabel.attributedText = timeAtt
        
        let titleAtt = NSMutableAttributedString.init(string: "加入了 ", attributes: titleAttDict)
        titleAtt.append(NSMutableAttributedString.init(string: responseModel?.currName ?? ""))
        titleLabel.attributedText = titleAtt
        codeImageView.sd_setImage(with: URL.init(string: XS_Singleton.shared.settingModel?.qrcode ?? ""), completed: nil)
        
        if courseClassification == "offline" {
            learnBtn.setTitle("订单查看", for: .normal)
        }
        
        let leftBgLayer = CAGradientLayer()
        leftBgLayer.colors = [UIColor(red: 0.93, green: 0.80, blue: 0.64, alpha: 1.00).cgColor, UIColor(red: 0.97, green: 0.91, blue: 0.82, alpha: 0.00).cgColor]
        leftBgLayer.locations = [0, 1]
        leftBgLayer.frame = leftNameView.bounds
        leftBgLayer.startPoint = CGPoint(x: 1.0, y: 0.5)
        leftBgLayer.endPoint = CGPoint(x: 0.0, y: 0.5)
        leftNameView.layer.addSublayer(leftBgLayer)
        
        let rightBgLayer = CAGradientLayer()
        rightBgLayer.colors = [UIColor(red: 0.97, green: 0.91, blue: 0.82, alpha: 0.00).cgColor, UIColor(red: 0.93, green: 0.80, blue: 0.64, alpha: 1.00).cgColor]
        rightBgLayer.locations = [0, 1]
        rightBgLayer.frame = rightNameView.bounds
        rightBgLayer.startPoint = CGPoint(x: 1.0, y: 0.5)
        rightBgLayer.endPoint = CGPoint(x: 0.0, y: 0.5)
        rightNameView.layer.addSublayer(rightBgLayer)
    }
    
    @objc func shareBtnAction(){
        let img: UIImage = UIImage.init(data: (XS_InviteVC.getImageFromView(view: contentView).pngData() ?? Data())) ?? addImage
        let shareItems:Array = [img]
        let activityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
        self.present(activityViewController, animated: true, completion: nil)
    }
    
    @objc func backBtnAction(){
//        learnBtnAction(UIButton())
        let vcs = navigationController?.viewControllers ?? []
        for subVC in vcs {
            if subVC.isKind(of: XS_KeChengVC.classForCoder()) || subVC.isKind(of: XS_KCXianxiaVC.classForCoder()) {
                navigationController?.popToViewController(subVC, animated: true)
                break
            }
        }
    }


    @IBAction func learnBtnAction(_ sender: UIButton) {
        if courseClassification == "offline" {
            //去到订单页面
            let orderVC = XS_OrderVC()
            navigationController?.pushViewController(orderVC, animated: true)
        }else{
            backBtnAction()
        }
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
